package com.car.advertising.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.car.advertising.CarApplication;
import com.car.advertising.R;
import com.car.advertising.activity.adapter.UpdatePasswordAdapter;
import com.car.advertising.connection.Contact;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.bean.PassWordBean;
import com.car.advertising.listener.SaveEditListener;
import com.support.widget.JumpManager;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.layout.BaseRecylerViewToolBarActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class UpdatePasswordActivity extends BaseRecylerViewToolBarActivity implements SaveEditListener {

    private List<PassWordBean> passWordList = new ArrayList<>();

    private String currentPwd = "";
    private String newPwd = "";
    private String critNewPwd = "";

    @Override
    protected void initToolBar() {
        setTitleClickable(false);
        setSubtitleClickable(true);
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle("修改登录密码");
        setToolBarTitleColor(R.color.black_bg);

        setPullDownEnabled(false);
        setPullUpEnabled(false);
    }

    @Override
    protected void init() {

        passWordList = getPassWordList();

        UpdatePasswordAdapter passwordAdapter = new UpdatePasswordAdapter(UpdatePasswordActivity.this, passWordList);
        initRecyclerView(passwordAdapter);
        passwordAdapter.setOnItemClickListener(passwordItemClick);
    }

    private List<PassWordBean> getPassWordList() {
        List<PassWordBean> arrayList = new ArrayList<>();
        arrayList.add(new PassWordBean("当前登录密码", "请输入原登录密码"));
        arrayList.add(new PassWordBean("新登录密码", "6-20位的登录密码"));
        arrayList.add(new PassWordBean("确认新登录密码", "请再次输入登录密码"));
        arrayList.add(new PassWordBean("6-20位数字、字母、符号、不允许有空格", "提交"));
        return arrayList;
    }

    private OnRecyclerViewItemClickListener passwordItemClick = new OnRecyclerViewItemClickListener() {

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            if (TextUtils.isEmpty(currentPwd)) {
                ToastUtil.show(UpdatePasswordActivity.this, "请输入当前密码", Toast.LENGTH_SHORT);
            } else if (TextUtils.isEmpty(newPwd)) {
                ToastUtil.show(UpdatePasswordActivity.this, "请输入新密码", Toast.LENGTH_SHORT);
            } else if (TextUtils.isEmpty(critNewPwd)) {
                ToastUtil.show(UpdatePasswordActivity.this, "请再次输入新密码", Toast.LENGTH_SHORT);
            } else if (newPwd.length() < 6 || newPwd.length() > 20) {
                ToastUtil.show(UpdatePasswordActivity.this, "新密码至少在6-20位字符以内", Toast.LENGTH_SHORT);
            } else if (critNewPwd.length() < 6 || critNewPwd.length() > 20) {
                ToastUtil.show(UpdatePasswordActivity.this, "新密码至少在6-20位字符以内", Toast.LENGTH_SHORT);
            } else if (!TextUtils.equals(newPwd, critNewPwd)) {
                ToastUtil.show(UpdatePasswordActivity.this, "两次新密码输入不一致", Toast.LENGTH_SHORT);
            } else {
                getRecordData(CarApplication.token, currentPwd, critNewPwd);
            }
        }
    };

    @Override
    public void getEditPositionText(int position, String string) {
        switch (position) {
            case 0:
                currentPwd = string;
//                ToastUtil.show(UpdatePasswordActivity.this, currentPwd, Toast.LENGTH_SHORT);
                break;
            case 1:
                newPwd = string;
//                ToastUtil.show(UpdatePasswordActivity.this, newPwd, Toast.LENGTH_SHORT);
                break;
            case 2:
                critNewPwd = string;
//                ToastUtil.show(UpdatePasswordActivity.this, critNewPwd, Toast.LENGTH_SHORT);
                break;
            default:
                break;
        }
    }

    private void getRecordData(String acToken, String password, String newPwd) {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("acToken", acToken));
        params.add(new OkHttpClientManager.Param("password", password));
        params.add(new OkHttpClientManager.Param("newPwd", newPwd));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_021, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(String request) {
                if (TextUtils.equals(request, "登录已过期")) {
                    JumpManager.gotoActivity(UpdatePasswordActivity.this, LoginActivity.class, false);
                    Contact.LoginAgain(UpdatePasswordActivity.this);
                    finish();
                }
            }

            @Override
            public void onResponse(String resp) {

            }

            @Override
            public void onFinish() {
            }
        }, params, null);
    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }


    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {

    }


    @Override
    protected void onRefreshFinish() {

    }

    @Override
    protected void onLoadMoreFinish() {

    }


}
